<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\MenuRepo;
use App\Repositories\ManagerRepo;


class MenuController extends BaseController
{
    public $menuRepo;
    public $managerRepo;

    public function _initialize()
    {
        $this->menuRepo = new MenuRepo;
        $this->managerRepo    = new ManagerRepo;
    }
    public function all()
    {
        return $this->sucRes(200,$this->menuRepo->allMenus());
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       if(MANAGER_ID){
            $manager =  $this->managerRepo->getManagerById(MANAGER_ID);
            $menus = $this->menuRepo->getMenus($manager->role_id,'children');
            return $this->sucRes(200,$menus);
       } else {
            return $this->sucRes(200,[]);
       }
       
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $data = $request->only(['pid','name','path','icon','title','order']);
        $this->menuRepo->addMenu($data);
        return $this->sucRes();
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
      
        $this->menuRepo->delMenu($id);
        return $this->sucRes();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $input = $request->only(['id','pid','name','path','title','icon','order']);
        $this->menuRepo->editMenu($input);
        return $this->sucRes();
    }
}
